package com.darren.euc.dao.impl;


import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.darren.euc.dao.FunctionDao;
import com.darren.euc.entity.Function;


@Repository
public class FunctionDaoImpl extends BaseDaoImpl<Function, Integer> implements FunctionDao {
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Function> getAll() {
		Session session = sessionFactory.getCurrentSession();	
		return (List<Function>)session.createQuery("from Function WHERE display = '1' AND functionId not like '7%' ").list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Function> getFunctionsByUserId(Integer id){	
		Session session = sessionFactory.getCurrentSession();
		String SQL = "SELECT * FROM euc_functions WHERE display = '1' AND function_id NOT LIKE '7%' AND id IN (SELECT	function_id FROM euc_role_function	WHERE	role_id IN (SELECT role_id FROM euc_user_role	WHERE	user_id = ?)) ";
		SQL += " UNION SELECT * FROM euc_functions WHERE display = '1'  AND function_id LIKE '8%' ";
		Query query = session.createSQLQuery(SQL).addEntity(Function.class);
		query.setParameter(0, id);
		return (List<Function>)query.list();
	}

	@Override
	public List<Function> findMemberFunctions() {
		Session session = sessionFactory.getCurrentSession();
		//超硬。。。
		String sql = "select * from euc_functions where display = '1' and (function_id = 70000 or parent_id = 70000)";
		Query query = session.createSQLQuery(sql).addEntity(Function.class);
		return (List<Function>)query.list();
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Function> findFunctionsByParentFunctionId(int i) {
		Session session = sessionFactory.getCurrentSession();
		String sql = "select * from euc_functions where display = '1' and parent_id = ? ";
		Query query = session.createSQLQuery(sql).addEntity(Function.class);
		query.setParameter(0, i);
		return (List<Function>)query.list();
	}

	@Override
	public List<Function> findFunctionsByFunctionId(int i) {
		Session session = sessionFactory.getCurrentSession();
		String sql = "select * from euc_functions where display = '1' and function_id = ? ";
		Query query = session.createSQLQuery(sql).addEntity(Function.class);
		query.setParameter(0, i);
		return (List<Function>)query.list();
	}

}